#coding=utf-8
#!/usr/bin/python
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
# See LICENSE for details.  wxPython2.8-win32-unicode-2.8.11.0-py26.exe
# cd /d D:\python2.6.6\dev_test 
# ..\python.exe wxp_menu_msg.py

""" 

PyWin32
wxPython

"""

import wx
import win32gui   
import win32con   
import time   
from winxpgui import *


class SparkTaskBarIcon(wx.TaskBarIcon):
    TBMENU_RESTORE = wx.NewId()
    TBMENU_CLOSE   = wx.NewId()
    TBMENU_CHANGE  = wx.NewId()
    TBMENU_REMOVE  = wx.NewId()
    
    def __init__(self, frame,picon,icon):
        wx.TaskBarIcon.__init__(self)
        self.frame = frame
        # Set the image
        #icon = self.MakeIcon(images.WXPdemo.GetImage())
        wc = win32gui.WNDCLASS()   
        hinst = wc.hInstance = win32gui.GetModuleHandle(None)  
        icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE

        #气泡提示的图标显示；这个地方有问题，就想知道这个图标和下面一句图标如何共用一个？
        hicon = LoadImage(hinst, icon, win32con.IMAGE_ICON, 0, 0, icon_flags)
               
       #任务栏设置个图标，这个图标可以显示菜单提示
        self.SetIcon(picon, "Spark")
        self.imgidx = 1
        
        # bind some events
        self.Bind(wx.EVT_TASKBAR_LEFT_DCLICK, self.OnTaskBarActivate)
        self.Bind(wx.EVT_MENU, self.OnTaskBarActivate, id=self.TBMENU_RESTORE)
        self.Bind(wx.EVT_MENU, self.OnTaskBarClose, id=self.TBMENU_CLOSE)
        #self.Bind(wx.EVT_MENU, self.OnTaskBarChange, id=self.TBMENU_CHANGE)
        self.Bind(wx.EVT_MENU, self.OnTaskBarRemove, id=self.TBMENU_REMOVE)
        
        #气泡提示注册win的窗口消息
        wc.lpszClassName = "Spark"  
        wc.lpfnWndProc = {win32con.WM_DESTROY: self.OnDestroy,}   
        classAtom = win32gui.RegisterClass(wc)   
        style = win32con.WS_OVERLAPPED | win32con.WS_SYSMENU   
        self.hwnd = win32gui.CreateWindow( classAtom, "Spark", style,   
                0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT,   
                0, 0, hinst, None)   
        #hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION)   
        nid = (self.hwnd, 0, win32gui.NIF_ICON, win32con.WM_USER+20, hicon, "Spark")   
        win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, nid)   

    def OnDestroy(self, hwnd, msg, wparam, lparam):   
        nid = (self.hwnd, 0)   
        win32gui.Shell_NotifyIcon(win32gui.NIM_DELETE, nid)   
        win32gui.PostQuitMessage(0) # Terminate the app. 
    
    def showMsg(self, title, msg):   
    
        nid = (self.hwnd, # 句柄   
                0, # 托盘图标ID   
                win32gui.NIF_INFO, # 标识   
                0, # 回调消息ID   
                0, # 托盘图标句柄   
                "Spark Message", # 图标字符串   
                msg, # 气球提示字符串   
                5, # 提示的显示时间   
                title, # 提示标题   
                win32gui.NIIF_INFO # 提示用到的图标   
                )   
        win32gui.Shell_NotifyIcon(win32gui.NIM_MODIFY, nid)  
    def CreatePopupMenu(self):
        """
        This method is called by the base class when it needs to popup
        the menu for the default EVT_RIGHT_DOWN event.  Just create
        the menu how you want it and return it from this function,
        the base class takes care of the rest.
        """
        menu = wx.Menu()
        menu.Append(self.TBMENU_RESTORE, "Restore spark")
        menu.Append(self.TBMENU_CLOSE,   "Close spark")
        menu.AppendSeparator()
        #menu.Append(self.TBMENU_CHANGE, "Change the TB Icon")
        menu.Append(self.TBMENU_REMOVE, "Remove Spark")
        return menu

    #def MakeIcon(self, img):
        #"""
        #The various platforms have different requirements for the
        #icon size...
        #"""
        #if "wxMSW" in wx.PlatformInfo:
            #img = img.Scale(16, 16)
        #elif "wxGTK" in wx.PlatformInfo:
            #img = img.Scale(22, 22)
        ## wxMac can be any size upto 128x128, so leave the source img alone....
        #icon = wx.IconFromBitmap(img.ConvertToBitmap() )
        #return icon
   
    def OnTaskBarActivate(self, evt):
        if self.frame.IsIconized():
            self.frame.Iconize(False)
        if not self.frame.IsShown():
            self.frame.Show(True)
        self.frame.Raise()

    def OnTaskBarClose(self, evt):
        wx.CallAfter(self.frame.OnClose)

    #def OnTaskBarChange(self, evt):
        #names = [ "WXPdemo", "Mondrian", "Pencil", "Carrot" ]                  
        #name = names[self.imgidx]
        
        #eImg = getattr(images, name)
        #self.imgidx += 1
        #if self.imgidx >= len(names):
            #self.imgidx = 0
            
        #icon = self.MakeIcon(eImg.Image)
        #self.SetIcon(icon, "This is a new icon: " + name)

    def OnTaskBarRemove(self, evt):
        self.RemoveIcon()